package com.fruits.controller;

import com.fruits.domain.Cart;
import com.fruits.domain.Fruit;
import com.fruits.service.CartService;
import com.fruits.service.FruitService;
import com.fruits.service.impl.CartServiceImpl;
import com.fruits.service.impl.FruitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/fruitServlet")
public class FruitServlet extends BaseServlet{
   private FruitService fruitService = new FruitServiceImpl();
    private CartService cartService = new CartServiceImpl();
    public String info(HttpServletRequest request, HttpServletResponse response) {
        String fidstr=request.getParameter("fid");
        int fid = Integer.parseInt(fidstr);
        Fruit fruit = fruitService.findByFid(fid);
        request.setAttribute("fruit", fruit);
         //判断
        String uidStr = request.getParameter("uid");
        int uid=Integer.parseInt(uidStr);
        if(uid!=0){
            Cart cart = cartService.find(uid, fid);
            if(cart!=null){
                if(cart.isCart()){
                    request.setAttribute("tit1", "已加入购物车");
                }
                if(cart.isStar()){
                    request.setAttribute("tit2", "已关注");
                }
            }
        }

        hot(request, response);

    //转发页面详情
        return "forward:/fruit_info.jsp";
    }

    public void hot(HttpServletRequest request, HttpServletResponse response) {

        List<Fruit> fruits = fruitService.findHot();
        request.setAttribute("fruits",fruits);


    }


}